package com.flying.compare.complator;

import com.flying.compare.Product;
import org.junit.Test;

import java.time.format.TextStyle;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 刘鹏飞
 * @ClassName CompatorTest
 * @Date 2024/12/28 21:05
 * @Created by 刘鹏飞
 * @Description 测试 定制排序
 */
public class ComparatorTest {

    @Test
    public void test1() {

        Product[] arr = new Product[5];
        arr[0] = new Product("HuaweiMate50pro", 6999.0);
        arr[1] = new Product("Apple15", 9999.0);
        arr[2] = new Product("XiaomiUltra", 6999.0);
        arr[3] = new Product("GalaxyS23Ultra", 5444.0);
        arr[4] = new Product("VivoX90pro", 5999.0);


        //        创建定制排序比较器
        // 比较器1: 按照价格,从低到高排序
        Comparator<Product> comparator = new Comparator<>() {
            @Override
            public int compare(Product o1, Product o2) {
                return Double.compare(o1.getPrice(), o2.getPrice());
            }
        };

        Arrays.sort(arr,comparator);

        for (Product product : arr) {
            System.out.println(product);
        }

        System.out.println("=============================================================================");
        // 比较器2: 根据名称字母排序
        Comparator<Product> comparator1 = Comparator.comparing(Product::getName);


        Arrays.sort(arr,comparator1);


        for (Product product : arr) {
            System.out.println(product);
        }

    }


}
